<?php

namespace App\Models;
use stdClass;
class UserModel extends BaseModel {

	public function __construct() {
		parent::__construct();
		$this -> _table = "User";
	}

	public function getUserInfo($username, $password) {
		$sql = sprintf("select * from `%s` where `username` = '%s' and `password` = '%s'", $this -> _table, $username, $password);
		return $this -> query($sql);
	}
	
	public function menu($user,$menu){ 
		$sql =
			"SELECT menu.ID,MenuName,menu.ControllerAction FROM user
			INNER JOIN role
			ON `user`.RoleID=role.ID
			INNER JOIN rolepower
			ON rolepower.RoleID=role.ID
			INNER JOIN power
			ON rolepower.Power=power.ID
			INNER JOIN menu
			ON menu.ID=ActionId
			WHERE UserName='" . $user . "' AND ActionType='" . $menu . "'";
			
			$sth = $this -> _dbHandle -> prepare($sql);
			$sth -> execute();
			return $result= $sth -> fetchAll(); 
		
	}
	
	public function getUserRolePower($user, $urlpath) {
		$sql = "select ControllerAction FROM user INNER JOIN rolepower on `user`.RoleID = rolepower.RoleID INNER JOIN power on rolepower.Power = power.ID WHERE UserName='" . $user . "' and ControllerAction='" . $urlpath . "'";
		return $this -> query($sql);
	}
	
	public function NameSelect($val){
		$sql="
		SELECT Time,`user`.ID, Username,age,`user`.RoleID,Email,`Password` 
		FROM `user`
		where Username LIKE '%".$val."%' ";
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $result= $sth -> fetchAll(); 
	}
	public function Cha($username){
		$sql = sprintf("select `Username`,age,Email,Phone,Password,Face from `%s` where `Username` = '%s'", $this -> _table, $username);
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $result= $sth -> fetchAll(); 
	}
	public function Pas($username,$data){
		$sql = sprintf("update `%s` set %s where `Username` = '%s'", $this -> _table, $this -> formatUpdate($data), $username);
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $result= $sth -> fetchAll(); 
	}
	public function UserItem(){
		$sql = sprintf("select * from `%s` %s", $this -> _table, $this -> filter);
		$time='order by time DESC';
		//        排序
		$order_column = $_GET['order']['0']['column']; // 哪一列排序
		$order_dir= $_GET['order']['0']['dir']; // ase desc 升序或者降序
		//拼接排序sql
		$orderSql = "";
		if($order_column>=1){
		    $i = intval($order_column);
		  switch($i){
		     case 2;$orderSql = " order by age ".$order_dir;break;
		    
		     default;$orderSql = '';
		  }
		   $sth = $this -> _dbHandle -> prepare($sql.$orderSql);
		}else{
			$sth = $this -> _dbHandle -> prepare($sql.$time);
			  
		}  
		$sth -> execute();
		$result= $sth -> fetchAll(); 
		
		//		分页
		$draw = $_GET["draw"];
		$start = $_GET["start"];
		$length = $_GET["length"];
		$newarr = new stdClass();
		$newarr->draw = $draw;
		$newarr->page = count($result)/$length;
		$newarr->recordsTotal = count($result);
		$newarr->recordsFiltered = count($result);
		$newarr->data = array();
		$h=0;
		for($i = $start; $i<$start+$length;$i++,$h++)
			{   if(!empty($result[$i])){
				$newarr->data[$h] = $result[$i];
				}else{
					break;
				}
						
			}
				
			echo json_encode($newarr);
		}
		
		
		
		/**/
		
		
}
